package com.wxzz.elearing.user.api.admin;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.user.dto.EnterpriseApplyDto;
import com.wxzz.elearing.user.dto.EnterpriseIdentityDto;
import com.wxzz.elearing.user.dto.EnterpriseOrganizationDto;
import com.wxzz.elearing.user.entity.AdminUser;
import com.wxzz.elearing.user.entity.EnterpriseIdentity;
import com.wxzz.elearing.user.entity.EnterpriseOrganization;
import com.wxzz.elearing.user.service.IEnterpriseOrganizationService;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/enterprise-organization")
@Api(tags = "组织架构(后台)")
public class EnterpriseOrganizationController {

    @Autowired
    private IEnterpriseOrganizationService enterpriseOrganizationService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "新增组织架构(后台)", notes = "新增组织架构(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveEnterpriseOrganization", method = RequestMethod.POST)
    public ReturnVO saveEnterpriseOrganization(@RequestHeader String token, @RequestBody EnterpriseOrganization enterpriseOrganization) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        enterpriseOrganization.setEnterpriseId(adminUser.getEnterpriseId());
        return enterpriseOrganizationService.saveEnterpriseOrganization(enterpriseOrganization);
    }

    @ApiOperation(value = "修改组织架构(后台)", notes = "修改组织架构(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "updateEnterpriseOrganization", method = RequestMethod.POST)
    public ReturnVO updateEnterpriseOrganization(@RequestHeader String token, @RequestBody EnterpriseOrganization enterpriseOrganization) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        enterpriseOrganization.setEnterpriseId(adminUser.getEnterpriseId());
        return enterpriseOrganizationService.updateEnterpriseOrganization(enterpriseOrganization);
    }

    @ApiOperation(value = "修改组织架构(后台)", notes = "修改组织架构(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "deleteEnterpriseOrganizationById/{enterpriseOrganizationId}", method = RequestMethod.POST)
    public ReturnVO deleteEnterpriseOrganizationById(@PathVariable Long enterpriseOrganizationId) {
        return enterpriseOrganizationService.deleteEnterpriseOrganizationById(enterpriseOrganizationId);
    }

    @ApiOperation(value = "根据pid展示组织架构(后台)", notes = "根据pid展示组织架构(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listEnterpriseOrganizationByPid", method = RequestMethod.POST)
    public ReturnVO<List<EnterpriseOrganization>> listEnterpriseOrganizationByPid(@RequestHeader String token, @RequestBody EnterpriseOrganizationDto enterpriseOrganizationDto) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        return enterpriseOrganizationService.listEnterpriseOrganizationByPid(adminUser.getEnterpriseId(),enterpriseOrganizationDto);
    }

    @ApiOperation(value = "新增组织身份(后台)", notes = "新增组织身份(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveEnterpriseIdentity", method = RequestMethod.POST)
    public ReturnVO saveEnterpriseIdentity(@RequestHeader String token, @RequestBody EnterpriseIdentity enterpriseIdentity) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        enterpriseIdentity.setEnterpriseId(adminUser.getEnterpriseId());
        return enterpriseOrganizationService.saveEnterpriseIdentity(enterpriseIdentity);
    }

    @ApiOperation(value = "修改组织身份(后台)", notes = "修改组织身份(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "updateEnterpriseIdentity", method = RequestMethod.POST)
    public ReturnVO updateEnterpriseIdentity(@RequestHeader String token, @RequestBody EnterpriseIdentity enterpriseIdentity) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        enterpriseIdentity.setEnterpriseId(adminUser.getEnterpriseId());
        return enterpriseOrganizationService.updateEnterpriseIdentity(enterpriseIdentity);
    }

    @ApiOperation(value = "根据pid展示组织身份(后台)", notes = "根据pid展示组织身份(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listEnterpriseIdentityByPid", method = RequestMethod.POST)
    public ReturnVO<List<EnterpriseIdentity>> listEnterpriseIdentityByPid(@RequestHeader String token, @RequestBody  EnterpriseIdentityDto enterpriseIdentityDto) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        return enterpriseOrganizationService.listEnterpriseIdentityByPid(adminUser.getEnterpriseId(),enterpriseIdentityDto);
    }

    @ApiOperation(value = "根据id删除组织身份(后台)", notes = "根据id删除组织身份(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "deleteEnterpriseIdentityById/{enterpriseIdentityId}", method = RequestMethod.POST)
    public ReturnVO deleteEnterpriseIdentityById(@PathVariable Long enterpriseIdentityId) {
        return enterpriseOrganizationService.deleteEnterpriseIdentityById(enterpriseIdentityId);
    }
}
